package net.mbl.ide.system.core;

import net.mbl.ide.utils.NetUtils;

public class CorePlaceInfo {
	long boxId;
	long slotId;
	long dspId;
	long coreId;
	byte[] ipAddr = new byte[] { (byte) 172, 16, 1, 1 };
	private long fNode;

	public void setIpAddr(byte[] ip) {
		if (ip != null && ip.length == 4) {
			for (int i = 0; i < ip.length; i++) {
				ipAddr[i] = ip[i];
			}
		}
		calcPlaceInfoFromIp();
	}

	public void setPlaceInfo(long boxId, long slotId, long dspId, long coreId) {
		this.boxId = boxId;
		this.slotId = slotId;
		this.dspId = dspId;
		this.coreId = coreId;
		calcIpFromPlaceInfo();
	}

	public long getBoxId() {
		return boxId;
	}

	public long getSlotId() {
		return slotId;
	}

	public long getDspId() {
		return dspId;
	}

	public long getCoreId() {
		return coreId;
	}

	private void calcPlaceInfoFromIp() {
		boxId = (ipAddr[2] & 0xff) >> 2;
		dspId = (ipAddr[2] & 0xff) & 0x3;
		slotId = (ipAddr[3] & 0xff) >> 4;
		coreId = (ipAddr[3] & 0xff) & 0xf;
		coreId--;
	}

	public byte[] calcIpFromPlaceInfo() {
		ipAddr[2] = (byte) ((boxId << 2) | dspId); // 第三位为机箱号左移2位或上DSP号
		ipAddr[3] = (byte) ((slotId << 4) | coreId); // 第四位为槽位号左移4位或上核号
		ipAddr[3]++;
		return ipAddr;
	}
	public byte[] getIp(){
		return ipAddr;
	}
	public String getIpStr(){
		return NetUtils.getIpStr(ipAddr);
	}
	
	public void setNode(long node){
		this.fNode = node;
	}
	public long getNode(){
		return this.fNode;
	}
	public String toString() {
		StringBuffer sb = new StringBuffer();
		sb.append(String.format("[%d,%d,%d,%d]:[%d,%d,%d,%d]", boxId, slotId,
				dspId, coreId, ipAddr[0] & 0xff, ipAddr[1] & 0xff,
				ipAddr[2] & 0xff, ipAddr[3] & 0xff));
		return sb.toString();
	}
	@Override
	public boolean equals(Object arg0){
		if(arg0 instanceof CorePlaceInfo){
			CorePlaceInfo c = (CorePlaceInfo) arg0;
			return (c.getBoxId()==this.getBoxId()) && (c.getSlotId() == this.getSlotId())
					&& (c.getDspId() == this.getDspId()) && (c.getCoreId() == this.getCoreId());
		}
		else
			return super.equals(arg0);
	}
	public long getValue(){
		return (boxId<<24)|(slotId<<16)|(dspId<<8|coreId);
	}
	public static void main(String[] args) {
		CorePlaceInfo cpi = new CorePlaceInfo();
		 cpi.setPlaceInfo(31, 2, 0, 0);
		 cpi.calcIpFromPlaceInfo();
		/*cpi.setIpAddr(new byte[] { (byte) 172, 16, 126, (byte) 180 });
		cpi.calcPlaceInfoFromIp();*/
		System.out.println(cpi.toString());
	}
}
